#include "CommentItemWidget.h"

CommentItemWidget::CommentItemWidget(QWidget *parent, const QString &username, const QString &time, const QString &comment)
    : QWidget{parent}
{
    QString finalComment = comment;
    if (finalComment.isEmpty())
    {
        int count = QRandomGenerator::global()->bounded(1, 200); // 生成 1~100 的随机数
        finalComment = QString("示例评论").repeated(count);
    }

    // 创建左侧头像
    avatar = new AvatarLabel(this);
    avatar->setFixedSize(50, 50);

    // 创建右侧信息部分
    usernameLabel = new QLabel(username, this);
    timeLabel = new QLabel(time, this);
    commentLabel = new QLabel(finalComment, this);
    commentLabel->setWordWrap(true); // 评论内容自动换行

    usernameLabel->setStyleSheet("color: #333; font-weight: bold;");
    timeLabel->setStyleSheet("color: #999; font-size: 12px;");

    commentLabel->setStyleSheet(
        "font-size: 14px;"
        "color: #555;"
        "background-color: #ffffff;"
        "border: 1px solid #e0e0e0;"
        "padding: 8px;"
        "border-radius: 6px;");

    // 设置用户名字体 加大加粗
    QFont usernameFont = usernameLabel->font();
    usernameFont.setBold(true);
    usernameFont.setPointSize(usernameFont.pointSize() + 2); // 比默认大2号字
    usernameLabel->setFont(usernameFont);

    // 设置用户名和时间在同一行
    QHBoxLayout *topLayout = new QHBoxLayout();
    topLayout->addWidget(usernameLabel);
    topLayout->addWidget(timeLabel);
    topLayout->addStretch();

    // 创建举报按钮
    QPushButton *reportButton = new QPushButton("❗ 举报", this);
    topLayout->addWidget(reportButton);

    // 右侧整体垂直布局
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addLayout(topLayout);    // 添加第一行
    rightLayout->addWidget(commentLabel); // 添加评论内容

    // 左右整体布局
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(avatar, 0, Qt::AlignTop); // 头像在顶部对齐
    mainLayout->addLayout(rightLayout);             // 右侧信息
    mainLayout->setContentsMargins(10, 5, 10, 5);   // 设置边距

    // 设置布局
    setLayout(mainLayout);

    // 设置评论边框
    this->setObjectName("commentItem"); // 使用对象名称选择器限定样式范围
    this->setAttribute(Qt::WA_StyledBackground);
    this->setStyleSheet(
        "#commentItem {" // 使用ID选择器限定范围
        "   background-color: white;"
        "   border: 1px solid #e0e0e0;"
        "   border-radius: 6px;"
        "}");

    connect(reportButton, &QPushButton::clicked, this, &CommentItemWidget::onReportClicked);
}
void CommentItemWidget::quickSetup(const Comment &comment)
{
    avatar->setPixmap(comment.avatar);
    usernameLabel->setText(comment.userName);
    timeLabel->setText(comment.time);
    commentLabel->setText(comment.comment);
}

void CommentItemWidget::onReportClicked()
{
    InputDialog dialog("举报评论", "举报理由", this);
    if (dialog.exec() == QDialog::Accepted)
    {
        QString reason = dialog.getInputText();
        if (!reason.isEmpty())
        {
            // TODO: 提交举报到服务器或本地处理
            QMessageBox::information(this, "提示", "举报提交成功！");
            qDebug() << "举报理由：" << reason;
        }
    }
}